// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: ProductServiceImpl.java

package com.market.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.market.bean.Product;
import com.market.bean.Supplier;

// Referenced classes of package com.market.service:
// BaseServiceImpl, IProductService

public class ProductServiceImpl extends BaseServiceImpl implements IProductService
{

    public ProductServiceImpl()
    {
    }

    public Product findById(long id)
    {
        return getProductDAO().findById(id);
    }

    public Product findById(Class cla, long id)
    {
        return getProductDAO().findById(cla, id);
    }

    public void delete(long id)
    {
        Product pro = getProductDAO().findById(id);
        if (pro != null)
        {
            Object o = getProductDAO().findById(getClassByCategory(pro.getCategory()), id);
            getProductDAO().delete(o);
        }
    }

    public List getNewList(String category, int max)
    {
        return getProductDAO().getNewList(category, max);
    }

    public List getHotList(String category, int max)
    {
        return getProductDAO().getHotList(category, max);
    }

    public List getDiscountList(int start, int max)
    {
        return getProductDAO().getDiscountList(start, max);
    }

    public List findBySupplier(long supplierid, int start, int max)
    {
        return getProductDAO().findBySupplier(supplierid, start, max);
    }

    public int getCountBySupplier(long supplierid)
    {
        return getProductDAO().getCountBySupplier(supplierid);
    }

    public int getCountByDiscount()
    {
        return getProductDAO().getCountByDiscount();
    }

    public List findSPPro(float price, String category)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(getClassByCategory(category));
        detachCri.add(Restrictions.between("price", new Float(price - 100F), new Float(price + 100F)));
        return getProductDAO().findByDetchedCriteria(detachCri, 0, 5);
    }

    public List findSSPro(Supplier sup)
    {
        return getProductDAO().findBySupplier(sup.getId(), 0, 5);
    }

    public List findSTPro(String type, String category)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(getClassByCategory(category));
        detachCri.add(Restrictions.eq("type", type));
        return getProductDAO().findByDetchedCriteria(detachCri, 0, 5);
    }

    public int getCount(Class cla)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(cla);
        return getProductDAO().getCountByDetachedCriteria(detachCri);
    }

    public List getProduct(Class cla, int start, int max)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(cla);
        return getProductDAO().findByDetchedCriteria(detachCri, start, max);
    }

    public List findByForm(String category,String type,String name,String parameter,String orderBy, int start, int max)
    {
        DetachedCriteria detachCri = getDetachCriteria( category, type, name, parameter, orderBy);
        return getProductDAO().findByDetchedCriteria(detachCri, start, max);
    }

    public int getCountByForm(String category,String type,String name,String parameter,String orderBy)
    {
        DetachedCriteria detachCri = getDetachCriteria( category, type, name, parameter, orderBy);
        return getProductDAO().getCountByDetachedCriteria(detachCri);
    }

    private DetachedCriteria getDetachCriteria(String category,String type,String name,String parameter,String orderBy)
    {
        //String category = sf.getCategory();
        DetachedCriteria detachCri = DetachedCriteria.forClass(getClassByCategory(category));
        if (!"all".equals(type) && !StringUtils.isEmpty(type))
            detachCri.add(Restrictions.eq("type", type));
        if (name != null && !StringUtils.isEmpty(name))
            detachCri.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
        if (!StringUtils.isEmpty(parameter))
            if ("rice".equals(category))
                detachCri.add(Restrictions.like("diameter", parameter, MatchMode.ANYWHERE));
            else if ("beer".equals(category))
                detachCri.add(Restrictions.like("range", parameter, MatchMode.ANYWHERE));
          
            else
                detachCri.add(Restrictions.like("range", parameter, MatchMode.ANYWHERE));
      
        if ("hot".equals(orderBy))
            detachCri.addOrder(Order.desc("hot"));
        else if ("adddate".equals(orderBy))
            detachCri.addOrder(Order.desc("adddate"));
        else if ("vipprice".equals(orderBy))
            detachCri.addOrder(Order.desc("vipprice"));
        else
            detachCri.addOrder(Order.desc("price"));
        return detachCri;
    }

    private Class getClassByCategory(String category)
    {
        if ("beer".equals(category))
            return com.market.bean.Ball.class;
        if ("rice".equals(category))
            return com.market.bean.Bowb.class;
      
        if ("safety".equals(category))
            return com.market.bean.Safety.class;
        else
            return com.market.bean.Bowa.class;
    }
}
